package forminrecipe.GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


import forminrecipe.core.Core;
import forminrecipe.data.*;

public class BrowserPanel extends JPanel {
	
	private Core coreRef;
	private GUI guiRef;
	
	private int selectedIndex = 0;
	
	private BoxLayout browserBoxLayout;

	private JScrollPane browserScrollPane;
	
	private JTable browserTable;
 
	private BoxLayout buttonBoxLayout;
	private JPanel buttonPanel;
	private JButton addCatagoryButton;
	private JButton saveNewRecipeButton;
	private JButton createNewRecipeButton;
	private JButton editRecipeButton;
	private JButton saveChangesButton;
	private JButton discardChangesButton;


	private BrowserTableModel browserTableModel;
	
	public BrowserPanel(Core core, GUI gui){
		coreRef=core;
		guiRef=gui;
		//browse panel
		browserBoxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(browserBoxLayout);
		browserScrollPane = new JScrollPane();
		
		browserTableModel = new BrowserTableModel();
		browserTable = new JTable();
		browserTable.setSize(constants.GUI_BROWSER_PANEL_TABLE_WIDTH, constants.GUI_BROWSER_PANEL_TABLE_HEIGHT);
		browserTable.setFillsViewportHeight(true);
			//browserTable.getColumnModel().getColumn(1).setMaxWidth(100);
			//browserTable.getColumnModel().getColumn(2).setMaxWidth(40);
			//browserTable.getColumnModel().getColumn(3).setMaxWidth(40);
			//browserTable.setCellSelectionEnabled(false);
			//browserTable.setRowSelectionAllowed(true);
			browserTable.addMouseListener(new MyMouseListener());
		browserScrollPane.add(browserTable);
		
		buttonPanel=new JPanel();
		buttonBoxLayout = new BoxLayout(this, BoxLayout.X_AXIS);
		addCatagoryButton=new JButton(constants.ADD_CATAGORY_BUTTON_TEXT);
		addCatagoryButton.addActionListener(new MyAction());
		saveNewRecipeButton = new JButton(constants.SAVE_NEW_RECIPE_BUTTON_TEXT);
		saveNewRecipeButton.addActionListener(new MyAction());
		saveNewRecipeButton.setVisible(false);
		createNewRecipeButton = new JButton(constants.CREATE_NEW_RECIPE_BUTTON_TEXT);
		createNewRecipeButton.addActionListener(new MyAction());
		editRecipeButton = new JButton(constants.EDIT_RECIPE_BUTTON_TEXT);
		editRecipeButton.addActionListener(new MyAction());
		saveChangesButton = new JButton(constants.SAVE_CHANGES_BUTTON_TEXT);
		saveChangesButton.addActionListener(new MyAction());
		saveChangesButton.setVisible(false);
		discardChangesButton = new JButton(constants.DISCARD_CHANGES_BUTTON_TEXT);
		discardChangesButton.addActionListener(new MyAction());
		discardChangesButton.setVisible(false);
		
		buttonPanel.add(addCatagoryButton);
		buttonPanel.add(saveNewRecipeButton);
		buttonPanel.add(createNewRecipeButton);
		buttonPanel.add(editRecipeButton);
		buttonPanel.add(saveChangesButton);
		buttonPanel.add(discardChangesButton);
		
		buttonPanel.setSize(400, 100);
		this.add(browserScrollPane);
		this.add(buttonPanel);
		
		this.setVisible(true);
	}
	
	public void addRecipeToList(RecipeData recipeData){
		browserTableModel.addRow(recipeData);	
	}
	
	public void setSelectedRecipe(int index){
		browserTable.setRowSelectionInterval(index, index);
		RecipeData recipeData = browserTableModel.getRecipeData(index);
		guiRef.displayRecipe(recipeData);
		coreRef.getIngredientsForDMB(browserTableModel.getID(index));
		selectedIndex = index;
	}
	
	public class MyMouseListener implements MouseListener{
		public void mouseReleased(MouseEvent e) {
			setSelectedRecipe(browserTable.getSelectedRow());
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub	
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub	
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub	
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub	
		}
	}
	
	public class MyAction implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	      String text = (String)e.getActionCommand();
	      
	      if(text == constants.ADD_CATAGORY_BUTTON_TEXT){
	    	  Object response = JOptionPane.showInputDialog(buttonPanel,
	              "Enter The Name Of New Food Catagory",
	             "Enter The Name Of New Food Catagory", JOptionPane.OK_CANCEL_OPTION
	             );
	    	  coreRef.addCatagoryGUI(response.toString());
	    	  coreRef.addCatagoryDBM(response.toString());
			
	      } else if (text == constants.SAVE_NEW_RECIPE_BUTTON_TEXT){
	    	  guiRef.saveRecipePassToRecipe();
	    	  createNewRecipeButton.setVisible(true);
	    	  saveNewRecipeButton.setVisible(false);
	      } else if (text == constants.CREATE_NEW_RECIPE_BUTTON_TEXT){
	    	  guiRef.resetRecipeFields();
	    	  guiRef.setFeildsEditability(true);
	    	  saveNewRecipeButton.setVisible(true);
	    	  createNewRecipeButton.setVisible(false);
	      } else if (text == constants.EDIT_RECIPE_BUTTON_TEXT){
	    	  guiRef.setFeildsEditability(true);
	    	  createNewRecipeButton.setVisible(false);
	    	  saveChangesButton.setVisible(true);
	    	  editRecipeButton.setVisible(false);
	    	  discardChangesButton.setVisible(true);
	      } else if (text == constants.SAVE_CHANGES_BUTTON_TEXT){
	    	  guiRef.setFeildsEditability(false);
	    	  saveChangesButton.setVisible(false);
	    	  editRecipeButton.setVisible(true);
	    	  createNewRecipeButton.setVisible(true);
	    	  discardChangesButton.setVisible(false);
	      }else if (text == constants.DISCARD_CHANGES_BUTTON_TEXT){
	    	  guiRef.setFeildsEditability(false);
	    	  saveChangesButton.setVisible(false);
	    	  editRecipeButton.setVisible(true);
	    	  discardChangesButton.setVisible(false);
	    	  createNewRecipeButton.setVisible(true);
	      }
	    }
	}

	public void refresh() {
		browserTable.setModel(browserTableModel);
		setSelectedRecipe(selectedIndex);
	}
}
